﻿/* License
 * 
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, Sylvain PONTOREAU (pontoreau.sylvain@outlook.com / sylvain.pontoreau.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Drawing;
using System.Xml.Serialization;
using Xlsx.ComponentModel.Enums;

namespace Xlsx.ComponentModel
{
    /// <summary>
    /// Xlsx skin.
    /// </summary>
    [Serializable]
    public sealed class XlsxSkin : ICloneable, IEquatable<XlsxSkin>
    {
        /// <summary>
        /// Get the Default XlsxSkin.
        /// </summary>
        public static XlsxSkin Default
        {
            get
            {
                return new XlsxSkin
                {
                    BorderColor = Color.Transparent,
                    BorderStyle = 0,
                    FillColor = Color.Transparent,
                    FontColor = Color.Black,
                    FontSize = 10,
                    HasBorder = false,
                    IsBold = false,
                    IsItalic = false,
                    IsUnderline = false
                };
            }
        }

        /// <summary>
        /// Font size.
        /// </summary>
        public int FontSize { get; set; }

        /// <summary>
        /// Font color.
        /// </summary>
        [XmlIgnore]
        public Color FontColor { get; set; }

        /// <summary>
        /// Html font color
        /// </summary>
        [XmlElement("FontColor")]
        public string HtmlFontColor
        {
            get
            {
                return String.Format("#{0:X2}{1:X2}{2:X2}", FontColor.R, FontColor.G, FontColor.B);
            }
            set
            {
                FontColor = ColorTranslator.FromHtml(value);
            }
        }

        /// <summary>
        /// True if font is bold.
        /// </summary>
        public bool IsBold { get; set; }

        /// <summary>
        /// True if font is italic.
        /// </summary>
        public bool IsItalic { get; set; }

        /// <summary>
        /// True if font is underline.
        /// </summary>
        public bool IsUnderline { get; set; }

        /// <summary>
        /// True if skin has border.
        /// </summary>
        public bool HasBorder { get; set; }

        /// <summary>
        /// Border style.
        /// </summary>
        public XlsxBorderStyle BorderStyle { get; set; }

        /// <summary>
        /// Border color.
        /// </summary>
        [XmlIgnore]
        public Color BorderColor { get; set; }

        /// <summary>
        /// Html border color
        /// </summary>
        [XmlElement("BoderColor")]
        public string HtmlBorderColor
        {
            get
            {
                return String.Format("#{0:X2}{1:X2}{2:X2}", BorderColor.R, BorderColor.G, BorderColor.B);
            }
            set
            {
                BorderColor = ColorTranslator.FromHtml(value);
            }
        }

        /// <summary>
        /// Cell fill color.
        /// </summary>
        [XmlIgnore]
        public Color FillColor { get; set; }

        /// <summary>
        /// Html fill color
        /// </summary>
        [XmlElement("FillColor")]
        public string HtmlFillColor
        {
            get
            {
                return String.Format("#{0:X2}{1:X2}{2:X2}", FillColor.R, FillColor.G, FillColor.B);
            }
            set
            {
                FillColor = ColorTranslator.FromHtml(value);
            }
        }

        /// <summary>
        /// Clone the skin.
        /// </summary>
        /// <returns>Cloned skin.</returns>
        public object Clone()
        {
            return new XlsxSkin
            {
                BorderColor = Color.FromArgb(BorderColor.A, BorderColor.R, BorderColor.G, BorderColor.B),
                BorderStyle = BorderStyle,
                FillColor = Color.FromArgb(FillColor.A, FillColor.R, FillColor.G, FillColor.B),
                FontColor = Color.FromArgb(FontColor.A, FontColor.R, FontColor.G, FontColor.B),
                FontSize = FontSize,
                HasBorder = HasBorder,
                IsBold = IsBold,
                IsItalic = IsItalic,
                IsUnderline = IsUnderline
            };
        }

        /// <summary>
        /// Returns a value that indicates whether the passing XlsxSkin is equal to this XlsxSkin.
        /// </summary>
        /// <param name="other">The other XlsxSkin.</param>
        /// <returns>true if the passing XlsxSkin is equal to this XlsxSkin; false otherwise.</returns>
        public bool Equals(XlsxSkin other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return FontSize == other.FontSize && FontColor.Equals(other.FontColor) && IsBold.Equals(other.IsBold) && IsItalic.Equals(other.IsItalic) && IsUnderline.Equals(other.IsUnderline) && HasBorder.Equals(other.HasBorder) && BorderStyle == other.BorderStyle && BorderColor.Equals(other.BorderColor) && FillColor.Equals(other.FillColor);
        }

        /// <summary>
        /// Returns a value that indicates whether the current instance is equal to a specified object.
        /// </summary>
        /// <param name="obj">Object to make the comparison with.</param>
        /// <returns>true if the current instance is equal to the specified object; false otherwise.</returns>        
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj is XlsxSkin && Equals((XlsxSkin) obj);
        }

        /// <summary>
        /// Gets the hash code for this object. 
        /// </summary>
        /// <returns>Hash code for this object.</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = FontSize;
                hashCode = (hashCode * 397) ^ FontColor.GetHashCode();
                hashCode = (hashCode * 397) ^ IsBold.GetHashCode();
                hashCode = (hashCode * 397) ^ IsItalic.GetHashCode();
                hashCode = (hashCode * 397) ^ IsUnderline.GetHashCode();
                hashCode = (hashCode * 397) ^ HasBorder.GetHashCode();
                hashCode = (hashCode * 397) ^ (int)BorderStyle;
                hashCode = (hashCode * 397) ^ BorderColor.GetHashCode();
                hashCode = (hashCode * 397) ^ FillColor.GetHashCode();
                return hashCode;
            }
        }

    }
}
